var stream = {
        isOpenUserMedia:false,
        mimeCodec:'video/webm; codecs=vp9,opus',
        isRecorder:false,
        time:100,
        topic:null,
        number:0,
        media : function(facingMode,v,topic) {
        	stream.topic=topic;
            // 调用用户媒体设备
            if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia
                || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
                // 访问摄像头或麦克风 ,facingMode:{exact:facingMode}
            	var videoConfig={ width: 960, height: 540};
            	if(facingMode!=""){
            		videoConfig.facingMode={"exact":facingMode};
            	}
                stream.getUserMedia({
                    video : videoConfig,
                    audio : true
                }, function(MediaStream) {
                    console.log("成功调用用户媒体设备");
                    v.srcObject = MediaStream;
                    v.play();
                    stream.mediaStream=MediaStream;
                    stream.createMediaRecorder(stream.topic);
                    // 订阅目标的消息
                    websocket.subscribe(stream.topic+"-toCollection",function(msg){
                    	var data=JSON.parse(msg.body);
                    	console.log(data);
                    	if(data.command=="createMediaRecorder"){
                    		if(stream.isRecorder){
                    			stream.isRecorder=false;
                    			try{
                    				stream.mediaRecorder.stop();
                    			}catch (e) {
								}
                    			console.log("反初始化成功");
                    		}
                    		stream.createMediaRecorder(stream.topic);
                    	}
                    });
                }, function(err) {
                    console.log(err);
                });
            }
        },
        // 推流
        createMediaRecorder:function(topic){
            var options = {
            	//audioBitsPerSecond : 128000,
                videoBitsPerSecond : 250000,
                mimeType : stream.mimeCodec
            }
            websocket.send(topic+"-toShow",JSON.stringify({"command":"createMediaSource"}));
            console.log("初始化成功");
            stream.mediaRecorder = new MediaRecorder(stream.mediaStream,options);
            stream.isRecorder=true;
            setTimeout(function(){
            	stream.number=0;
            	stream.mediaRecorder.start(stream.time);
            },1000);
            stream.mediaRecorder.ondataavailable = function(e) {
            	// 将内存里面的视频流数据转成ArrayBuffer
                var reader = new FileReader();
                reader.onloadend=function() {
                	if(reader.result.byteLength>0){
                        var buf = new Uint8Array(reader.result);
                        //console.log("send video bytes "+buf.byteLength);
                        // 发送视频流信息
                        if(stream.isRecorder){
                        	websocket.send(topic,buf,true,{"number":stream.number});
                        	stream.number++;
                        }
                    }
                    reader=null;
                }
                reader.readAsArrayBuffer(e.data);
            }
        },
        // 访问用户媒体设备的兼容方法
        getUserMedia : function (constrains, success, error) {
            if(navigator.mediaDevices.getUserMedia){
                // 最新标准API
                navigator.mediaDevices.getUserMedia(constrains).then(success).catch(error);
            } else if (navigator.webkitGetUserMedia){
                // webkit内核浏览器
                navigator.webkitGetUserMedia(constrains).then(success).catch(error);
            } else if (navigator.mozGetUserMedia){
                // Firefox浏览器
                navigator.mozGetUserMedia(constrains).then(success).catch(error);
            } else if (navigator.getUserMedia){
                // 旧版API
                navigator.getUserMedia(constrains).then(success).catch(error);
            }
        }
    }
function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
        "SymbianOS", "Windows Phone",
        "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}